/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { HvigorNode } from '@ohos/hvigor';
import { OhosModuleContext, Target } from '@ohos/hvigor-ohos-plugin/src/plugin/context/plugin-context';

/**
 * Base structure for annotation analysis results
 * Contains essential metadata extracted from source files
 */
export interface BaseAnalyzeResult {
  name: string; // Name of the class/component
  annotation: string; // Annotation tag found
  sourceFilePath: string; // Path to the source file
}

/**
 * Base extension context interface
 *
 * This interface defines the basic shared properties that all extensions can access.
 * It provides access to the Hvigor build system node, module context, and other
 * shared data necessary for plugin execution.
 */
export interface BaseExtensionContext {
  /** The Hvigor node representing the module being processed */
  readonly node: HvigorNode;

  /** The module context provided by the Hvigor build system */
  readonly moduleContext: OhosModuleContext;

  /** Current file analysis results (read-only) */
  readonly currentView: ReadonlyArray<BaseAnalyzeResult>;

  /** The current build target being processed, if any */
  currentTarget?: Target;

  moduleIgnored?: boolean;

  /** List of source files to be scanned for annotations */
  scanFiles: string[];
}

/**
 * Main extension context interface
 *
 * This interface extends the base context with additional methods for managing
 * analyze results and template data.
 * It provides a unified way for extensions
 * to access shared data and store extension-specific data.
 */
export interface ExtensionContext extends BaseExtensionContext {
  /**
   * Get all analysis results of the specified type
   *
   * @template T - Type parameter extending BaseAnalyzeResult
   * @returns {Set<T>} Set of analyze results cast to the specified type
   */
  getAnalyzeResults<T extends BaseAnalyzeResult = BaseAnalyzeResult>(): Set<T>;

  /**
   * Add one or more analyze results to the shared collection
   *
   * @param {BaseAnalyzeResult | BaseAnalyzeResult[]} results - The result to add
   */
  addAnalyzeResults(results: BaseAnalyzeResult | BaseAnalyzeResult[]): void;

  /**
   * Get template data for a specific component
   *
   * @param {string} componentName - The name of the component
   * @returns {Record<string, any> | undefined} The template data or undefined if none exists
   */
  getTemplateData(componentName: string): Record<string, any> | undefined;

  /**
   * Get the entire template data map
   *
   * @returns {Map<string, Record<string, any>>} Map of component names to template data
   */
  getTemplateDataMap(): Map<string, Record<string, any>>;

  /**
   * Add template data for a specific component
   *
   * @param {string} componentName - The name of the component
   * @param {Record<string, any>} data - The template data to add
   */
  addTemplateData(componentName: string, data: Record<string, any>): void;
}
